package com.stone.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stone.bean.AppResponse;
import com.stone.bean.BasePage;
import com.stone.bean.dto.UserAddDTO;
import com.stone.bean.dto.UserUpdateDTO;
import com.stone.constant.Const;
import com.stone.entity.SysUser;
import com.stone.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 超管用户的处理
 */
@Slf4j
@RestController
@RequestMapping("/backstage/sysUser")
@Api(tags = "系统管理员")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @PostMapping("/queryById")
    @ApiOperation("查询")
    public AppResponse<List<SysUser>> getUserInfoByUserId(BasePage dto) {
        Page<SysUser> userPage = new Page<>(dto.getPage(), dto.getPageSize());
        IPage<SysUser> page = sysUserService.page(userPage, new QueryWrapper<SysUser>().orderByDesc("id"));
        return AppResponse.success().setRecords(page.getRecords()).setTotal(page.getTotal());
    }

    @PostMapping("/insert")
    @ApiOperation("保存")
    public AppResponse addUser(@RequestBody @Valid UserAddDTO dto, @RequestHeader(value = Const.AUTH) String token) {
        dto.setToken(token);
        sysUserService.addUser(dto);
        return AppResponse.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public AppResponse updateUser(@RequestBody @Valid UserUpdateDTO dto, @RequestHeader(value = Const.AUTH) String token) {
        dto.setToken(token);
        sysUserService.updateUser(dto);
        return AppResponse.success();
    }

    @DeleteMapping("/delete/{userId}")
    @ApiOperation("删除")
    public AppResponse delete(@PathVariable(value = "userId") Long userId) {
        sysUserService.removeById(userId);
        return AppResponse.success();
    }

    @PostMapping("/reset/{userId}")
    @ApiOperation("重置密码")
    public AppResponse setPassword(@PathVariable(value = "userId") Long userId, @RequestHeader(value = Const.AUTH) String token) {
        sysUserService.reSetPassword(userId, token);
        return AppResponse.success();
    }
}
